package top.xinsix.est.service.impl;

import org.springframework.beans.BeanUtils;
import top.xinsix.est.dto.UsersDto;
import top.xinsix.est.mapper.UsersMapper;
import top.xinsix.est.pojo.Users;
import top.xinsix.est.service.UsersService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import top.xinsix.est.dto.Page;
import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;
import top.xinsix.est.tools.UUIDUtils;
import top.xinsix.est.tools.redis.RedisKeyUtils;
import top.xinsix.est.tools.redis.RedisUtils;

import javax.annotation.Resource;

@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private RedisUtils redisUtils;

    public Users getUsersById(Long id){
        return usersMapper.getUsersById(id);
    }

    public List<Users>	getUsersListByMap(Map<String,Object> param){
        return usersMapper.getUsersListByMap(param);
    }

    public Integer getUsersCountByMap(Map<String,Object> param){
        return usersMapper.getUsersCountByMap(param);
    }

    public Integer qdtxAddUsers(Users users){
            users.setCreatdTime(new Date());
            return usersMapper.insertUsers(users);
    }

    public Integer qdtxModifyUsers(Users users){
        users.setUpdatedTime(new Date());
        return usersMapper.updateUsers(users);
    }

    public Integer qdtxDeleteUsersById(Long id){
        return usersMapper.deleteUsersById(id);
    }

    public Integer qdtxBatchDeleteUsers(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return usersMapper.batchDeleteUsers(param);
    }

    public Page<Users> queryUsersPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = usersMapper.getUsersCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Users> usersList = usersMapper.getUsersListByMap(param);
        page.setList(usersList);
        return page;
     }

    public UsersDto loginByPhone(String phone) {
        //根据手机号获取用户信息
        UsersDto usersDto=usersMapper.getUsersByPhone(phone);
        //验证数据中是否存在用户信息，如果不存在，注册，添加用户信息
        if(usersDto==null){
            Users users=new Users();
            users.setAccount(phone);
            users.setIsAuthentication(0);
            users.setCreatdTime(new Date());
            users.setIsDelete(0);
            usersMapper.insertUsers(users);
            //生成token
            //将用户信息和token存在redis中
            setToken(users);
        }else{
            //生成token
            //将用户信息和token存在redis中
            setToken(usersDto);
        }
        //设置验证码失效
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS,Constants.Sms.TYPE_REGIST_OR_LOGIN.toString(), phone);
        redisUtils.delete(key);
        return usersDto;
    }

    public UsersDto loginByWechat(String accessToken, String openId) {
        //根据手机号获取用户信息
        UsersDto usersDto=usersMapper.getUsersByOpenId(openId);
        //验证数据中是否存在用户信息，如果不存在，注册，添加用户信息
        if(usersDto==null){
            Users users=new Users();
            users.setOpenId(openId);
            users.setIsAuthentication(0);
            users.setCreatdTime(new Date());
            users.setBindStatus(1);
            users.setBindTime(new Date());
            users.setIsDelete(0);
            usersMapper.insertUsers(users);
            //生成token
            //将用户信息和token存在redis中
            setToken(users);
        }else{
            //生成token
            //将用户信息和token存在redis中
            setToken(usersDto);
        }
        //设置验证码失效
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS,Constants.Sms.TYPE_REGIST_OR_LOGIN.toString(), openId);
        redisUtils.delete(key);
        return usersDto;
    }

    public void setToken(Users user){
        UsersDto usersDto = new UsersDto();
        BeanUtils.copyProperties(user,usersDto);
        setToken(usersDto);
    }
    public void setToken(UsersDto usersDto) {
        String token = UUIDUtils.getUUID32();
        usersDto.setToken(token);
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
        redisUtils.setJson(key, usersDto, Constants.Duration.HALF_AN_HOUR);
    }

    public Users loginByAccount(String account, String password) {
        return null;
    }

    public UsersDto getLoginUser(String token) {
        return null;
    }
}
